from django.views.decorators.csrf import csrf_exempt
from rest_framework.decorators import api_view

from psychometry.service import PsychometryService
from utils.authorize import authorize
from utils.exception_handler import my_exception_handler
from utils.request import params
from utils.response import success

@csrf_exempt
@api_view(["POST"])
@my_exception_handler
@authorize
@params(required=["qid"])
def create_psychometry(request):
	uid = request.session["user"]["uid"]
	result = PsychometryService().create_psychometry(uid, **request.params)
	return success(result, "创建成功")

@csrf_exempt
@api_view(["POST","PUT"])
@my_exception_handler
@authorize
@params(required=["pid", "answers"])
def ans_pasychometry(request):
	result = PsychometryService().answer(**request.params)
	return success(result, "提交成功")

@csrf_exempt
@api_view(["GET"])
@my_exception_handler
@authorize
@params(optional=["limit=20", "offset=0"])
def get_psychometrys(request):
	uid = request.session["user"]["uid"]
	result = PsychometryService().get_psychometrys(uid, **request.params)
	return success(result, "获取测试记录成功")

@csrf_exempt
@api_view(["GET"])
@my_exception_handler
@authorize
def get_psychometry(request, pid):
	result = PsychometryService().get_psychometry(pid)
	return success(result, "获取测试详情成功")

@csrf_exempt
@api_view(["GET"])
@my_exception_handler
@authorize
def del_psychometry(request, pid):
	result = PsychometryService().delete_psychometry(pid)
	return success(result, "删除测试记录成功")

